﻿using MVCDriver.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCDriver.Controllers
{
    public class DriverController : Controller
    {
        // GET: Driver
        TextEntities db = new TextEntities();

        public ActionResult Index()
        {
            ViewData.Model = db.DriverInfo.AsEnumerable();
            return View();
        }
        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            var viewModel = new DriverEditViewMODEL();
            ViewBag.TruckID = GetClassInfo();
            return View(viewModel);
        }
        /// <summary>
        /// 提交添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(DriverEditViewMODEL model)
        {
            //1.模型验证
            if (ModelState.IsValid)
            {
                //2.如果验证通过，写入新数据
                //2.1实例化数据访问上下文
                using (var db = new TextEntities())
                {
                    var entity = new DriverInfo()
                    {
                        AddTime = DateTime.Now,
                        Name = model.Name,
                        Age = model.Age,
                        Mobile = model.Mobile,
                        TruckID = model.TruckID,
                    };

                    //2.2调用Add方法
                    db.DriverInfo.Add(entity);
                    //2.3提交操作
                    db.SaveChanges();
                    ViewBag.Msg = "新学员添加成功！";

                    return RedirectToAction("Index");
                }
            }
            else
            {   //3.如果验证不通过，消息提示
                ModelState.AddModelError("", "您输入的信息有误！");
            }
            ViewBag.ClassInfo = GetClassInfo();
            return View(model);
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            //1.实例化数据访问上下文
            using (var db = new TextEntities())
            {
                var viewModel = new DriverEditViewMODEL();
                //2.根据id查询对应的学员信息
                var Driver = db.DriverInfo.FirstOrDefault(s => s.DriverID == id);
                if (Driver != null)
                {
                    //3.构造视图需要的实体
                    viewModel = new DriverEditViewMODEL()
                    {
                        Age = Driver.Age,
                        TruckID = (int)Driver.TruckID,
                        id = Driver.DriverID,
                        Mobile = Driver.Mobile,
                        Name = Driver.Name
                    };
                }
                ViewBag.ClassInfo = GetClassInfo();
                return View(viewModel);
            }
            //ViewBag.TruckID = new SelectList(db.TruckInfo, "TruckID", "Number");
            //ViewData.Model = db.DriverInfo.Find(id);//将查询结果通过ViewData.Model传到前端
            //return View();
        }

        /// <summary>
        /// 提交修改
        /// </summary>
        /// <param name="driver"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(DriverEditViewMODEL viewModel)
        {
            //模型验证
            if (ModelState.IsValid)
            {
                //1.实例化数据访问上下文
                using (var db = new TextEntities())
                {
                    //2.查询出当前修改的学员
                    var model = db.DriverInfo.FirstOrDefault(s => s.DriverID == viewModel.id);
                    //3.对学员的属性赋值
                    if (model != null)
                    {
                        model.Age = viewModel.Age;
                        model.TruckID = viewModel.TruckID;
                        model.Mobile = viewModel.Mobile;
                        model.Name = viewModel.Name;
                        //4.提交操作
                        db.SaveChanges();
                        ViewBag.Msg = "信息修改成功！";
                        return RedirectToAction("Index");
                    }

                }
            }
            else
            {
                ModelState.AddModelError("", "您输入的信息有误！");
            }
            ViewBag.ClassInfo = GetClassInfo();
            return View(viewModel);

            //db.Entry(driver).State = System.Data.Entity.EntityState.Modified;
            //db.SaveChanges();
            //return RedirectToAction("Index");
            ////连接到其他控制器下的方法 return RedirectToAction("Index","Home"); 
            ////使用这个方法会报错 return Redirect("index");
        }
        #endregion


        #region   
        public ActionResult Details(int id)
        {
            ViewData.Model = db.DriverInfo.Find(id);
            return View();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int id)
        {
            //1.实例化数据访问上下文
            using (var db = new TextEntities())
            {
                var result = new ResultModel();
                //2.查询当前删除的学员是否存在
                var model = db.DriverInfo.FirstOrDefault(s => s.DriverID == id);
                if (model != null)
                {
                    //3.如果存在则删除
                    //3.1调用Remove方法标记为删除
                    db.DriverInfo.Remove(model);
                    //3.2提交操作
                    if (db.SaveChanges() > 0)
                    {
                        result.IsSuccess = true;
                        result.ErrorMessage = "删除成功";
                    }
                    else
                    {
                        result.IsSuccess = false;
                        result.ErrorMessage = "网络繁忙，请稍后重试";
                    }
                }
                else
                {
                    //4.如果不存在则提示错误消息
                    result.IsSuccess = false;
                    result.ErrorMessage = "当前的学员已经删除！";

                }
                return Json(result);
            }

            //DriverInfo drinfo = db.DriverInfo.Find(id);
            //db.DriverInfo.Remove(drinfo);
            //db.SaveChanges();
            //return RedirectToAction("Index");
        }
        #endregion


        /// <summary>
        /// 获取学员的班级信息
        /// </summary>
        /// <returns></returns>
        private List<SelectListItem> GetClassInfo()
        {
            //1.实例化数据访问上下文
            using (var db = new TextEntities())
            {
                //2.查询学员班级信息
                //3.构造学员班级信息数据
                var classInfo = db.TruckInfo.Where(c => true).Select(c => new SelectListItem
                {
                    Text = c.Number,
                    Value = c.TruckID.ToString()
                }).ToList();
                return classInfo;

            }
        }
    }
}